package com.example.administrator.officeplatform.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.bean.ReimbursementVoInfo;
import com.example.administrator.officeplatform.bean.SettlementInfo;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/24.
 */

public class SettlementAdatper extends RecyclerView.Adapter{
    public static interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private SettlementAdatper.OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(SettlementAdatper.OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private List<SettlementInfo> list = new ArrayList<>();
    private Context mContext;

    public SettlementAdatper(List<SettlementInfo> list, Context mContext) {
        this.list = list;
        this.mContext=mContext;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_settlement, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new PersonViewHolder(view);    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final PersonViewHolder personViewHolder = (PersonViewHolder) holder;
        personViewHolder.position = position;
        SettlementInfo bmInfo = list.get(position);
        DecimalFormat format = new DecimalFormat("0.00");

        personViewHolder.tv_dh.setText("报销单号："+bmInfo.getNumber());
        personViewHolder.tv_bxlx.setText("报销类型："+bmInfo.getShow_type());
        personViewHolder.tv_bxr.setText("报销人："+bmInfo.getReimbursementName());
        personViewHolder.tv_ssbm.setText("所属部门："+bmInfo.getDepartmentName());
        personViewHolder.tv_fyssbm.setText("费用所属部门："+bmInfo.getBearDepartmentName());
        personViewHolder.tv_mc.setText("员工/供应商名称："+bmInfo.getShow_supplierRole());
        personViewHolder.tv_jslb.setText("结算类别："+bmInfo.getShow_settleCategory());
        personViewHolder.tv_xm.setText("费用项目："+bmInfo.getCostItem());
        personViewHolder.tv_fyzq.setText("费用所属期间："+bmInfo.getPeriod());
        personViewHolder.tv_jszq.setText("预计结算期间："+bmInfo.getEstimatedSettleTime());
        if (bmInfo.getReimbursementAmout()!=null){
            personViewHolder.tv_bxje.setText("报销金额："+"￥"+format.format(bmInfo.getReimbursementAmout()));
        }
        if (bmInfo.getTempBorrowing()!=null){
            personViewHolder.tv_zjk.setText("暂借款："+"￥"+format.format(bmInfo.getTempBorrowing()));
        }
        if (bmInfo.getSettleAmout()!=null){
            personViewHolder.tv_yjs.setText("应结算金额："+"￥"+format.format(bmInfo.getSettleAmout()));
        }
        if (bmInfo.getRewardAmout()!=null){
            personViewHolder.tv_jlje.setText("奖励金额："+"￥"+format.format(bmInfo.getRewardAmout()));
        }
        if (bmInfo.getDebitAmout()!=null){
            personViewHolder.tv_kkje.setText("扣款金额："+"￥"+format.format(bmInfo.getDebitAmout()));
        }
        if (bmInfo.getActualSettleAmout()!=null){
            personViewHolder.tv_sjjs.setText("实际结算金额："+"￥"+format.format(bmInfo.getActualSettleAmout()));
        }
//        if (bmInfo.getCleared()!=null){
//            personViewHolder.tv_yjsje.setText("已结算金额："+"￥"+format.format(bmInfo.getCleared()));
//        }
//        personViewHolder.tv_yjsje.setVisibility(View.GONE);
        if (bmInfo.getOilcardSettleAmout()!=null){
            personViewHolder.tv_ykje.setText("油卡金额："+"￥"+format.format(bmInfo.getOilcardSettleAmout()));
        }
        if (bmInfo.getCashAmout()!=null){
            personViewHolder.tv_xjje.setText("现金金额："+"￥"+format.format(bmInfo.getCashAmout()));
        }
        BigDecimal SettleAmou = bmInfo.getSettleAmout();
        BigDecimal Oil = bmInfo.getOilcardSettleAmout();
        BigDecimal Cash = bmInfo.getCashAmout();
        BigDecimal Cleared  = bmInfo.getCleared();

        if (bmInfo.getSettleAmout()==null){
            SettleAmou = new BigDecimal(0);

        }
        if (bmInfo.getOilcardSettleAmout()==null){
             Oil = new BigDecimal(0);

        }
        if (bmInfo.getCashAmout()==null){
             Cash = new BigDecimal(0);

        }
        if (bmInfo.getCleared()==null){
             Cleared = new BigDecimal(0);

        }
        BigDecimal syje = SettleAmou.subtract(Oil).
                subtract(Cash).subtract(Cleared);

//        personViewHolder.tv_syjs.setText("剩余结算金额："+format.format(syje));
//        personViewHolder.tv_syjs.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public View rootView;
        public int position;
        public TextView tv_dh,tv_bxlx,tv_bxr,tv_ssbm,tv_mc,tv_jslb,tv_xm,tv_fyzq,tv_jszq,tv_bxje
                ,tv_zjk,tv_yjsje,tv_jlje,tv_kkje,tv_sjjs,tv_yjs,tv_ykje,tv_xjje,tv_syjs,tv_fyssbm;
        public PersonViewHolder(View itemView) {
            super(itemView);
            tv_dh = (TextView)itemView.findViewById(R.id.tv_dh);
            tv_bxlx = (TextView)itemView.findViewById(R.id.tv_bxlx);
            tv_bxr = (TextView)itemView.findViewById(R.id.tv_bxr);
            tv_ssbm = (TextView)itemView.findViewById(R.id.tv_ssbm);
            tv_mc = (TextView)itemView.findViewById(R.id.tv_mc);
            tv_jslb = (TextView)itemView.findViewById(R.id.tv_jslb);
            tv_xm = (TextView)itemView.findViewById(R.id.tv_xm);
            tv_fyzq = (TextView)itemView.findViewById(R.id.tv_fyzq);
            tv_jszq = (TextView)itemView.findViewById(R.id.tv_jszq);
            tv_bxje = (TextView)itemView.findViewById(R.id.tv_bxje);
            tv_zjk = (TextView)itemView.findViewById(R.id.tv_zjk);
//            tv_yjsje = (TextView)itemView.findViewById(R.id.tv_yjsje);
            tv_jlje = (TextView)itemView.findViewById(R.id.tv_jlje);
            tv_kkje = (TextView)itemView.findViewById(R.id.tv_kkje);
            tv_sjjs = (TextView)itemView.findViewById(R.id.tv_sjjs);
            tv_yjs = (TextView)itemView.findViewById(R.id.tv_yjs);
            tv_ykje = (TextView)itemView.findViewById(R.id.tv_ykje);
            tv_xjje = (TextView)itemView.findViewById(R.id.tv_xjje);
//            tv_syjs = (TextView)itemView.findViewById(R.id.tv_syjs);
            tv_fyssbm = (TextView)itemView.findViewById(R.id.tv_fyssbm);


            rootView = itemView.findViewById(R.id.ll);
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(position);
            }
            return false;
        }
    }
}
